﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Profile;
using System.Data;
using Gift.Model.ENUM;
using Gift.DAL;
using Gift.Model;

namespace WebSite.admin.product
{
    public partial class brand_add : BaseAdmin
    {
        readonly DALBrand theBrand = new DALBrand();
        tbBrand oBrand;

        bool UploadPhoto()
        {

                string fileName = this.fileBrandPhoto.ShortFileName;
                fileName = fileName.Replace(":", "_").Replace(" ", "_").Replace("\\", "_").Replace("/", "_");
                fileName = DateTime.Now.Ticks.ToString() + "_" + fileName;
                if (this.fileBrandPhoto.HasFile)
                {
                    this.fileBrandPhoto.SaveAs(Server.MapPath("~/uploadfile/product/" + fileName));
                }
                this.lblPhotoUrl.Text = fileName;
                return true;
   
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.ValidateLoginRule(ERule.AddProductClass);
                if (base.Request.QueryString["bid"] != null)
                {
                    this.upbind();
                }
            }
        }

        private void upbind()
        {
            oBrand = theBrand.GetModel(int.Parse(Request.QueryString["bid"]));
            if (oBrand != null)
            {

                this.txtBrandName.Text = oBrand.brandName;
                this.txtOrder.Text = oBrand.brandOrder.ToString();
                this.isVisible.Checked = oBrand.visible;
                if (oBrand.brandLogo != null)
                {
                    this.lblPhotoUrl.Text = oBrand.brandLogo.ToString();
                    this.fileBrandPhoto.Required = false;
                    this.fileBrandPhoto.ShowRedStar = false;
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (base.Request.QueryString["bid"] != null)
            {
                if (this.fileBrandPhoto.FileName.Trim().Length > 0)
                {
                    this.UploadPhoto();
                }
                oBrand = theBrand.GetModel(int.Parse(Request.QueryString["bid"]));
                oBrand.brandName = this.txtBrandName.Text.Trim();
                oBrand.brandOrder = int.Parse(this.txtOrder.Text);
                oBrand.brandLogo = this.lblPhotoUrl.Text;
                oBrand.visible = this.isVisible.Checked;
                theBrand.Update(oBrand);

                ExtAspNet.Alert.Show("商品品牌修改成功！", "", "openNewTab('ProductBrand','product/brand.aspx','品牌列表','AddProductBrand','AddProductBrand')");
            }
            else
            {
                this.UploadPhoto();
                oBrand = new tbBrand();
                oBrand.brandName = this.txtBrandName.Text.Trim();
                oBrand.brandOrder = int.Parse(this.txtOrder.Text);
                oBrand.brandLogo = this.lblPhotoUrl.Text;
                oBrand.visible = this.isVisible.Checked;
                oBrand.addTime = DateTime.Now;
                theBrand.Add(oBrand);
                ExtAspNet.Alert.Show("商品品牌添加成功！", "", "openNewTab('ProductBrand','product/brand.aspx','品牌列表','AddProductBrand')");
            }
        }


    }
}